import { AnimationFrame, CubeTexture, TransformNode, Utils } from 'babylon-lib/index';
import { RawAssetsName, RawAssetsUrl } from '../../raw-assets-info';
import { DefaultStage } from '../default-stage';
import { Mesh } from '@babylonjs/core/Meshes/mesh';
import { PBRMaterial } from '@babylonjs/core/Materials/PBR/pbrMaterial';
import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
import { Helper } from 'babylon-lib/tool/helper';


class Asset {

    public static initialize(stage: DefaultStage) {
        this.setModel(stage);
        this.setTexture(stage);
        this.setMaterial(stage);
    }

    public static buildingNodes: Array<TransformNode>;
    public static buildingMeshes: Array<Mesh>;
    public static boundMeshes: Array<Mesh> = [];

    public static setModel(stage: DefaultStage) {
        const nodes = stage.loader(stage.mainScene).getMeshTaskInfo(RawAssetsUrl.modelBuilding)?.loadedTransformNodes!;
        const meshes = stage.loader(stage.mainScene).getMeshTaskInfo(RawAssetsUrl.modelBuilding)?.loadedMeshes! as Array<Mesh>;

        this.buildingNodes = nodes;
        this.buildingMeshes = meshes;
        Helper.nodeInArray(RawAssetsName.collider, meshes, this.boundMeshes, false);
    }

    public static cubeTexture: CubeTexture;

    public static setTexture(stage: DefaultStage) {
        this.cubeTexture = stage.loader(stage.mainScene).getCubeTextureTaskInfo(RawAssetsUrl.lightCubePX)?.texture!;
    }

    public static setMaterial(stage: DefaultStage) {
        this.buildingMeshes.forEach(m => {
            const material = m.material as PBRMaterial;
            if (material) {
                material.reflectionTexture = this.cubeTexture;
            }

        });
    }

}


export { Asset }